package fdupc.crypto.aes;

import lombok.extern.slf4j.Slf4j;

import javax.crypto.Cipher;
import java.nio.charset.Charset;

/**
 * 类描述:  Aes-128-CBC增强加密解密处理工具
 *
 * @author 王雷
 * @version 1.0.0
 * @date 2023/1/31 0031 下午 4:28
 */
@Slf4j
public class AesCbcEnhanceUtil extends AbstractAesBase {

    /**
     * 功能名称: Aes-128-CBC增强加密解密处理工具
     *
     * @description: 构造方法
     * @author: XXSD
     * @date: 2023-06-30 20:48
     */
    protected AesCbcEnhanceUtil() {

    }

    /**
     * 功能名称: Aes-128-CBC增强加密解密处理工具——工厂
     *
     * @return Aes-128-CBC增强加密解密处理工具对象
     * @description:
     * @author: XXSD
     * @date: 2023-06-30 20:47
     */
    public static IAesManager factory() {
        return new AesCbcEnhanceUtil();
    }

    /**
     * 功能描述：加密处理
     *
     * @param srcStr  明文
     * @param charset 字符集，默认utf8
     * @param srcKey  密钥
     * @param iv      向量
     * @date: 2023/1/31 0031 下午 5:42
     */
    @Override
    protected String doEncrypt(String srcStr, Charset charset, String srcKey, String iv) throws Exception {
        Cipher cipher = giveCipher();
        int blockSize = cipher.getBlockSize();
        byte[] dataBytes = srcStr.getBytes(charset);
        int plaintextLength = dataBytes.length;

        if (plaintextLength % blockSize != 0) {
            plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
        }

        byte[] plaintext = new byte[plaintextLength];
        System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);

        return super.dataEncrypt(cipher, plaintext, charset, srcKey, iv);
    }

//    /**
//     * 解密
//     *
//     * @param srcStr  密文
//     * @param charset 字符集，默认utf8
//     * @param srcKey  密钥
//     * @param iv      向量
//     * @return 明文
//     */
//    @Override
//    protected String doDecrypt(String srcStr, Charset charset, String srcKey, String iv) throws Exception {
//        //先用base64解密
//        byte[] encrypted1 = decodeBase64(srcStr);
//
//        Cipher cipher = giveCipher();
//        SecretKeySpec keyspec = new SecretKeySpec(srcKey.getBytes(charset), "AES");
//        IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());
//
//        cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
//
//        byte[] original = cipher.doFinal(encrypted1);
//        return new String(original, charset).trim();
//    }

    /*public static void main(String[] values) throws Exception {
//        String scrKey = "jkl;POIU1234++==";
        IAesManager iam = AesCbcEnhanceUtil.factory();
        String scrKey = iam.giveKeyData(32);
        String iv = iam.giveIvData(16);
        System.out.println(scrKey);
        System.out.println(iv);
        final String encrypt = iam.encrypt("Test测试——kdidiso-;safasdfas", StandardCharsets.UTF_8, scrKey, iv);
        log.error("加密结果：{}", encrypt);
        final String decrypt = iam.decrypt(encrypt, StandardCharsets.UTF_8, scrKey, iv);
        log.error("解密结果：{}", decrypt);
    }*/
}
